package com.ksd.modules.special.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.members.service.IAdminUserService;
import com.ksd.modules.members.vo.AdminUserVo;
import com.ksd.modules.special.entity.AdminSpecialUserBuy;
import com.ksd.modules.special.mapper.AdminSpecialUserBuyMapper;
import com.ksd.modules.special.po.AdminSpecialUserBuySavePO;
import com.ksd.modules.special.po.AdminSpecialUserBuySearchPO;
import com.ksd.modules.special.service.IAdminSpecialService;
import com.ksd.modules.special.service.IAdminSpecialUserBuyService;
import com.ksd.modules.special.vo.AdminSpecialUserBuyVo;
import com.ksd.modules.special.vo.AdminSpecialVo;
import com.ksd.utils.snow.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminSpecialUserBuyServiceImpl extends BaseServiceImpl<AdminSpecialUserBuyMapper, AdminSpecialUserBuy> implements IAdminSpecialUserBuyService {

    @Autowired
    private IAdminUserService adminUserService;
    @Autowired
    private IAdminSpecialService adminSpecialService;
    
    /**
     * 查询列表
     * @param adminSpecialUserBuySearchPO
     * @return
     */
    public Page<AdminSpecialUserBuyVo> findAdminSpecialUserBuys(AdminSpecialUserBuySearchPO adminSpecialUserBuySearchPO){
        Page<AdminSpecialUserBuy> page = new Page<>(adminSpecialUserBuySearchPO.getPageNo(),adminSpecialUserBuySearchPO.getPageSize());
        LambdaQueryWrapper<AdminSpecialUserBuy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSpecialUserBuy::getSystemId,SystemIdThrealLocal.get().getSystemId());
        // 订单编号
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminSpecialUserBuySearchPO.getOrderno()),AdminSpecialUserBuy::getOrderno,adminSpecialUserBuySearchPO.getOrderno());
        // 支付方式
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialUserBuySearchPO.getPayMethod()),AdminSpecialUserBuy::getPayMethod,adminSpecialUserBuySearchPO.getPayMethod());
        // 具体资源订单
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialUserBuySearchPO.getOpid()),AdminSpecialUserBuy::getOpid,adminSpecialUserBuySearchPO.getOpid());
        // 兑换码
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminSpecialUserBuySearchPO.getCode()),AdminSpecialUserBuy::getCode,adminSpecialUserBuySearchPO.getCode());
        // uuid
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminSpecialUserBuySearchPO.getUuid()),AdminSpecialUserBuy::getUuid,adminSpecialUserBuySearchPO.getUuid());
        // 手机
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminSpecialUserBuySearchPO.getPhone()), AdminSpecialUserBuy::getPhone,adminSpecialUserBuySearchPO.getPhone());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminSpecialUserBuySearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                            AdminSpecialUserBuy::getNickname, adminSpecialUserBuySearchPO.getKeyword())
                    .or().like(AdminSpecialUserBuy::getUsername, adminSpecialUserBuySearchPO.getKeyword())
                    .or().like(AdminSpecialUserBuy::getTitle, adminSpecialUserBuySearchPO.getKeyword())

            );
        }
        // 排序
        lambdaQueryWrapper.orderByDesc(AdminSpecialUserBuy::getCreateTime);
        Page<AdminSpecialUserBuy> adminSpecialPage = this.page(page, lambdaQueryWrapper);
        Page<AdminSpecialUserBuyVo> adminSpecialVoPage = tranferPageBo(adminSpecialPage, AdminSpecialUserBuyVo.class);
        return adminSpecialVoPage;
    }

    /**
     * 保存和更新
     * @param adminSpecialUserBuySavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public AdminSpecialUserBuyVo saveUpdateAdminSpecialUserBuy(AdminSpecialUserBuySavePO adminSpecialUserBuySavePO){
        AdminSpecialUserBuy adminSpecial = tranferBo(adminSpecialUserBuySavePO, AdminSpecialUserBuy.class);
        adminSpecial.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminSpecial.setSystemName(SystemIdThrealLocal.get().getSystemName());
        this.saveOrUpdate(adminSpecial);
        AdminSpecialUserBuyVo adminSpecialVo = tranferBo(adminSpecial, AdminSpecialUserBuyVo.class);
        return adminSpecialVo;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminSpecialUserBuyVo getAdminSpecialUserBuyById(Long id){
        AdminSpecialUserBuy adminSpecial = this.getById(id);
        AdminSpecialUserBuyVo adminSpecialVo = tranferBo(adminSpecial, AdminSpecialUserBuyVo.class);
        return adminSpecialVo;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminSpecialUserBuyById(Long id){
        boolean b = this.removeById(id);
        return b;
    }


    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminSpecialUserBuy(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        return b;
    }

    /**
     * 用户绑定专题订单
     * @param uuid
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean bindUserOrder(String uuid, Long opid){
        LambdaQueryWrapper<AdminSpecialUserBuy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSpecialUserBuy::getUuid,uuid);
        lambdaQueryWrapper.eq(AdminSpecialUserBuy::getOpid,opid);
        AdminSpecialUserBuy adminSpecialUserBuy = this.getOne(lambdaQueryWrapper);
        if(adminSpecialUserBuy == null) {
            adminSpecialUserBuy = new AdminSpecialUserBuy();
            AdminUserVo adminUserVo = adminUserService.getAdminUserByUUId(uuid);
            AdminSpecialVo adminSpecialVo = adminSpecialService.getAdminSpecialById(opid);
            adminSpecialUserBuy.setOpid(opid);
            adminSpecialUserBuy.setUserId(adminUserVo.getId());
            adminSpecialUserBuy.setNickname(adminUserVo.getNickName());
            adminSpecialUserBuy.setUsername(adminUserVo.getUserName());
            adminSpecialUserBuy.setAvatar(adminUserVo.getAvatar());
            adminSpecialUserBuy.setUuid(adminUserVo.getUuid());
            adminSpecialUserBuy.setPhone(adminUserVo.getTelephone());
            adminSpecialUserBuy.setAddress(adminUserVo.getAddress());

            adminSpecialUserBuy.setTitle(adminSpecialVo.getTitle());
            adminSpecialUserBuy.setDescription(adminSpecialVo.getDescription());
            adminSpecialUserBuy.setCover(adminSpecialVo.getImg());
            adminSpecialUserBuy.setCode("");
            adminSpecialUserBuy.setPrice(Objects.nonNull(adminSpecialVo.getPrice())?adminSpecialVo.getPrice(): String.valueOf(adminSpecialVo.getCoin()));

            adminSpecialUserBuy.setOrderno(String.valueOf(new SnowflakeIdWorker(7,8).nextId()));
            adminSpecialUserBuy.setTradeno(String.valueOf(new SnowflakeIdWorker(8,8).nextId()));
            adminSpecialUserBuy.setPayMethod(4);
            adminSpecialUserBuy.setPayMethodName("人工");

            adminSpecialUserBuy.setSystemId(SystemIdThrealLocal.get().getSystemId());
            adminSpecialUserBuy.setSystemName(SystemIdThrealLocal.get().getSystemName());
            adminSpecialUserBuy.setOrderJson("");
            return this.saveOrUpdate(adminSpecialUserBuy);
        }
        return false;
    }
}
